<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">

          <el-col :lg="4" :md="12">
            <el-form-item label="真实姓名:">
              <el-input
                clearable
                v-model="where.realname"
                placeholder="请输入真实姓名"/>
            </el-form-item>
          </el-col>

          <el-col :lg="5" :md="12">
            <el-form-item label="身份证号:">
              <el-input
                clearable
                v-model="where.idcard"
                placeholder="请输入身份证号"/>
            </el-form-item>
          </el-col>

          <el-col :lg="4" :md="12">
            <el-form-item label="认证结果:">
              <el-select
                clearable
                v-model="where.status"
                placeholder="请选择类型"
                class="ele-fluid">
                <el-option label="全部" value=""/>
                <el-option label="未实名" value="0"/>
                <el-option label="成年" value="1"/>
                <el-option label="未成年" value="2"/>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">

            <el-form-item label="创建时间:">
              <el-date-picker
                style="width: 100%"
                v-model="where.time"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss"
                label-format="yyyy-MM-dd HH:mm:ss"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>

          </el-col>

          <el-col :lg="9" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">

        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:role:edit')">修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此数据吗？"
            @confirm="remove(row)">
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:role:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>
        <!-- 状态列 -->
        <template slot="status" slot-scope="{row}">
          <el-switch
            v-model="row.status"
            @change="editStatus(row)"
            :active-value="1"
            :inactive-value="2"/>
        </template>

        <template slot="status" slot-scope="{row}">

          <el-tag v-if="row.status === 0" type="warning" size="small">未实名</el-tag>
          <el-tag v-if="row.status === 1" type="success" size="small">成年</el-tag>
          <el-tag v-if="row.status === 2" type="" size="small">未成年</el-tag>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <!-- <role-edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"/> -->
    <!-- 权限分配弹窗 -->
    <!-- <role-auth
      :data="current"
      :visible.sync="showAuth"/> -->
  </div>
</template>

<script>
import { mapGetters } from "vuex";
// import RoleEdit from './role-edit.vue';
// import RoleAuth from './role-auth.vue';

export default {
  name: 'BoxUserAuth',
  // components: {RoleEdit},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/auth/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'uid',
          label: '用户ID',
          width: 100,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'realname',
          label: '真实姓名',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 200,
          formatter: (row, column, cellValue) => {
            return this.$formatDataName(cellValue)
          }
        },

        {
          prop: 'idcard',
          label: '身份证件号码',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 200,
          formatter: (row, column, cellValue) => {
            return this.$formatDataIdCard(cellValue)
          }
        },
        {
          prop: 'nickname',
          label: '用户昵称',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 200
        },
        {
          prop: 'phone',
          label: '登录手机号',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 200,
          formatter: (row, column, cellValue) => {
            return this.$formatDataPhone(cellValue)
          }
        },

        {
          prop: 'auth_type_name',
          label: '认证方式',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 200
        },
        {
          prop: 'status',
          label: '认证结果',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 200,
          slot: 'status'
        },
        {
          prop: 'reason',
          label: '认证未通过原因',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 200
        },


        {
          prop: 'create_time',
          label: '创建时间',
          sortable: 'custom',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 160,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },

      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showAuth: false
    };
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 显示分配权限 */
    openAuth(row) {
      this.current = row;
      this.showAuth = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/boxBanConfig/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的数据吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/boxBanConfig/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "status": row.status
      })
      this.$http.post('/boxBanConfig/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message({type: 'success', message: res.data.msg});
        } else {
          row.status = !row.status ? 1 : 0;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style scoped>
</style>
